package com.df.springmvc.model.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.SN_GUID;
import com.common.util.Date.DateUtil;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.df.springmvc.model.dao.WorkFlowDao;
import com.df.springmvc.supper.SupperServices;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class WorkFlowServices extends SupperServices{
	@Autowired
	WorkFlowDao dao;
	
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		org.apache.log4j.Logger.getLogger(getClass()).info("参数信息:" + paramMap);
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		/**
		 */
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("ApplyId","<center>请假编号</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("rootCause","<center>请假原因</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("applytype","<center>请假类型</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("applyer","<center>请假人</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("startTime","<center>开始时间</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("endTime","<center>结束时间</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("applyStatus","<center>申请状态</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("days","<center>请假天数</center>",DhtmlGridFieldBean.TYPE_ED,"150"));
		/**
		 * 增加对应的业务代码
		 */
		gridBean.setAttribuate(attribuate);
		ArrayList<Object[]> list=(ArrayList<Object[]>)dao.queryApplyList();
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	@Override
	public Object add(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		// ////////////////////////////保存时,需关联流程////////////////////////////////
		String id="" + paramMap.get("ApplyId");
		if(dao.check(id)){
			HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
			map.put("guid",SN_GUID.getUUID());
			map.put("status","1");
			map.put("createby",getCurrentUserBean().getUserId());
			map.put("createtime",new Date());
			map.put("updateby",getCurrentUserBean().getUserId());
			map.put("updatetime",new Date());
			map.put("ApplyId","AP_" + DateUtil.getInstance().setDateFormat("yyyyMMdd_HHmmssSSS"));
			map.put("applyStatus","草稿");
			dao.add(map);
		}else{
			throw new Exception("已经存在");
		}
		return super.add(paramMap);
	}
	public Object doSubmit(HashMap<String,String> paramMap) throws Exception{
		return dao.doSubmit(paramMap);
	}
	@Override
	public Object upd(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
		map.put("updateby",getCurrentUserBean().getUserId());
		map.put("updatetime",new Date());
		Object rs=dao.upd(map);
		return rs;
	}
	@Override
	public Object del(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		int successCount=0;
		int errorCount=0;
		for(String item:("" + paramMap.get("guid")).split(",")){
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("guid",item);
			if("0".equals(dao.del(map))){
				errorCount++;
			}else{
				successCount++;
			}
		}
		return "成功[" + successCount + "]失败[" + errorCount + "]";
	}
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		return dao.view(guid);
	}
	public Object doApply1(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.doApply1(paramMap);
	}
	public Object doApply2(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.doApply2(paramMap);
	}
	public Object doApply3(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.doApply3(paramMap);
	}
	public Object doPersonnel(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.doPersonnel(paramMap);
	}
	public Object doFinancial(HashMap<String,String> paramMap) throws Exception{
		return dao.doFinancial(paramMap);
	}
	public Object doArchive(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.doArchive(paramMap);
	}
}
